<template>
  <div>
    <div class="flex flex-col h-full">
      <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
        <a-form ref="formRef" class="flex-1" :model="formState" :label-col="{ style: { width: '70px' } }">
          <a-form-item name="pyjm" label="" class="w-[240px]">
            <a-input v-model:value="formState.pyjm" placeholder="诊室编号/诊室名称" />
          </a-form-item>
        </a-form>
      </SearchContainer>
      <a-card class="mt-2 flex-1">
        <div class="flex justify-between">
          <a-button type="primary" class="mb-3 mr-2" @click="onEditOpen()">新增</a-button>
          <BaseExport type="运维" name="诊室维护" code="00001186" :search-params="formState">导出</BaseExport>
        </div>
        <BaseTable
          :loading="loading"
          :data-source="tableData"
          :pagination="pagination"
          bordered
          row-key="id"
          fill
          column-code="00001186"
          @change="(page) => (pagination = page)"
        >
          <template #bodyCell="{ column, record, index }">
            <template v-if="column.dataIndex === 'xh'">
              {{ index + 1 }}
            </template>
            <template v-if="column.dataIndex === 'action'">
              <a-button type="link" size="small" @click="onEditOpen(record)"> 修改 </a-button>
              <a-divider type="vertical" />
              <ConfirmModal description="确定删除?" @confirm="onDel(record)">
                <a-button danger type="text" size="small"> 删除 </a-button>
              </ConfirmModal>
            </template>
          </template>
        </BaseTable>
      </a-card>
    </div>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuccess" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import ConfirmModal from '@/components/ConfirmModal'
import Edit from './Edit.vue'

const rowData = ref({})
const editOpen = ref(false)
const formRef = ref(null)
const formState = ref({ pyjm: '' })

const onEditOpen = (record = {}) => {
  rowData.value = { ...record }
  editOpen.value = true
}

const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/opex/getRoomList',
  immediate: true,
  params: formState.value
})

const onDel = async (record) => {
  const res = await post('/operation/opex/doDelRoomInfo', {
    id: record.id
  })

  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}

const onSuccess = () => {
  editOpen.value = false
  getData()
}
onMounted(() => {
  getData()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
